<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    okText="保存并安排任务"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading" class="drawer-content">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="任务类名"
          hasFeedback >
          <a-input autocomplete="off" placeholder="请输入任务类名" v-decorator="['jobClassName', {initialValue:'cn.jtfadmin.core.modules.quartz.service.impl.SpringJob',rules: [{ required: true, message: '请输入任务类名!' }]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="cron表达式">
<!--          <a-input autocomplete="off" placeholder="请输入cron表达式" v-decorator="['cronExpression', {'initialValue':'0/1 * * * * ?',rules: [{ required: true, message: '请输入任务类名!' }]}]" />-->
<!--          <a target="_blank" href="http://cron.qqe2.com/">-->
<!--            <a-icon type="share-alt" />-->
<!--            在线cron表达式生成-->
<!--          </a>-->
          <ak-cron ref="innerVueCron" v-decorator="['cronExpression', { initialValue: '* * * * * ? *', rules:[{ required: true, message: '请选择表达式!'}] }]" @change="setCorn"></ak-cron>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="参数"
          hasFeedback >
          <a-input autocomplete="off" placeholder="请输入参数" v-decorator="['parameter', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="描述"
          hasFeedback >
          <a-input autocomplete="off" placeholder="请输入描述" v-decorator="['description', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="过期策略">
          <a-radio-group buttonStyle="solid" v-decorator="[ 'misfireInstructionAction', {'initialValue':'FIRE_NOW'}]">
            <a-radio-button :value="'FIRE_NOW'">立即执行</a-radio-button>
            <a-radio-button :value="'DO_NOTHING'">不处理</a-radio-button>
            <a-radio-button :value="'IGNORE_MISFIRE_POLICY'">忽略过期，按照未过期执行</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始时间">
          <ak-date  v-decorator="[ 'startTime']" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" :disabled-time="false"></ak-date>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="结束时间">
          <ak-date  v-decorator="['endTime']" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" :disabled-time="false"></ak-date>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <a-radio-group buttonStyle="solid" v-decorator="[ 'status', {'initialValue':'NORMAL'}]">
            <a-radio-button :value="'NORMAL'">正常</a-radio-button>
            <a-radio-button :value="'PAUSED'">停止</a-radio-button>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { postAction, putAction } from '../../../api/manage'
  import AkCron from '../../../components/akfh/AkCron'
  import pick from 'lodash.pick'
  // import moment from "moment"
  import {sysQuartzJobUrl} from '../../../api/url'
  import AkDate from "../../../components/akfh/AkDate";

  export default {
    name: "QuartzJobModal",
    components:{AkDate, AkCron},
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: sysQuartzJobUrl
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'jobClassName','cronExpression','parameter','description','status', 'startTime', 'endTime'));
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk: function () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let formData = Object.assign(this.model, values)
            let httpurl = ''
            let pro = null;
            if (!this.model.id) {
              httpurl += this.url.add
              pro = postAction(httpurl, formData)
            } else {
              httpurl += this.url.edit
              pro = putAction(httpurl, formData)
            }

            //时间格式化
            pro.then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()

              }
            }).finally(() => {
              that.confirmLoading = false

            })

          }
        })
      },
      handleCancel () {
        this.close()
      },
      setCorn(data){
        this.$nextTick(() => {
          this.model.cronExpression = data;
        });

        if (Object.keys(data).length==0) {
          this.$message.warning('请输入cron表达式!');
        }
      },
      validateCron(rule, value, callback){
        if(!value){
          callback()
        }else if (Object.keys(value).length==0) {
          callback("请输入cron表达式!");
        }
      },

    }
  }
</script>

<style scoped>

</style>